package com.rfid.afd.presenter;

import android.os.Handler;

import com.rfid.afd.activity.listener.ModelListener;
import com.rfid.afd.activity.view.IShunjiaView;
import com.rfid.afd.model.ShunjiaModel;
import com.rfid.db.bean.Rfid;
import com.rfid.jni.RfidDecoder;
import com.rfid.util.DBManager;
import com.rfid.util.RfidManager;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ShunjiaPresenter extends BasePresenter<IShunjiaView> implements ModelListener {

    public ShunjiaModel mModel;
    protected ExecutorService mExecutor;
    protected Handler mHandler;
    public ShunjiaPresenter() {
        mExecutor = Executors.newSingleThreadExecutor();
        mHandler = new Handler();
        mModel = new ShunjiaModel();
        mModel.setRfidListener(this);
    }

    public void  getRfidsByIndexRanger(Rfid startIndex,Rfid endIndex) {
        mIview.showLoading();
        mExecutor.execute(new Runnable() {
            @Override
            public void run() {
                List<Rfid> rfids = mModel.getRfidsByIndexRanger(startIndex,endIndex);
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mIview.onRfids(rfids);
                        mIview.stopLoading();
                    }
                });
            }
        });
    }

    public void shunjia(String shelf,List<Rfid> rfids,Rfid start,Rfid end) {
        if(start == null || end == null) {
            return;
        }
        mExecutor.execute(new Runnable() {
            @Override
            public void run() {
                mModel.addShunjiaLevel(start.getRindex(),end.getRindex(),shelf);
                mModel.shunjia(shelf,rfids);
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mIview.onShunjiaComplete();
                    }
                });
            }
        });
    }

    public void startScanRfid() {
        mModel.startScan();
    }

    public void stopScanRfid() {
        mModel.stopScan();
    }

    @Override
    public void onRfid(String rfid) {
        boolean shelf = false;
        if(rfid.startsWith("shelf")) {
            System.out.println("rfid = " + rfid);
            rfid = rfid.replace("shelf","");
            shelf = true;
        }
        System.out.println("shelf = " + shelf + ",rfid = " +rfid);
        Rfid rfid1 = null;
        if(!shelf) {
            rfid1 = DBManager.queryRfidByUid(rfid);
        }
        boolean finalShelf = shelf;
        String finalRfid = rfid;
        Rfid finalRfid1 = rfid1;
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if(finalShelf) {
                    mIview.onShelf(finalRfid);
                } else {
                    mIview.onScaned(finalRfid, finalRfid1);
                }
            }
        });
    }

    @Override
    public void onRfid(String epc, String barcode) {
    }
}
